package com.zmsk.zmio;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.zmsk.zmio.constant.Const;
import com.zmsk.zmio.inter.IIOHandler;

public class ZMIOReceiver extends BroadcastReceiver {

    private static final String TAG = "ZMIOReceiver";

    IIOHandler mIOHandler = null;

    public ZMIOReceiver() {
        Log.d(TAG, "ZMIOReceiver inited1.");
        mIOHandler = IOHandlerFactory.getInstance(ApplicationUtils.getContext());
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "ZMIOReceiver received2.");
        if (mIOHandler == null) {
            mIOHandler = IOHandlerFactory.getInstance(ApplicationUtils.getContext());
        }
        if (intent != null && mIOHandler != null) {
            String action = intent.getAction();
            if (Const.ACTION_GATE_OPEN.equals(action)) {
                String idNumber = intent.getStringExtra("idNumber");
                mIOHandler.noticeGateOpen(context, idNumber);
            } else if (Const.ACTION_GATE_CLOSE.equals(action)) {
                mIOHandler.noticeGateClose(context);
            } else if (Const.ACTION_INFRARED_LED_ON.equals(action)) {
                mIOHandler.noticeInfraredLedOn(context);
            } else if (Const.ACTION_INFRARED_LED_OFF.equals(action)) {
                mIOHandler.noticeInfraredLedOff(context);
            } else if (Const.ACTION_WHITE_LED_ON.equals(action)) {
                mIOHandler.noticeWhiteLedOn(context);
            } else if (Const.ACTION_WHITE_LED_OFF.equals(action)) {
                mIOHandler.noticeWhiteLedOff(context);
            } else if (Const.ACTION_RED_LED_ON.equals(action)) {
                mIOHandler.noticeRedLedOn(context);
            } else if (Const.ACTION_RED_LED_OFF.equals(action)) {
                mIOHandler.noticeRedLedOff(context);
            } else if (Const.ACTION_GREEN_LED_ON.equals(action)) {
                mIOHandler.noticeGreenLedOn(context);
            } else if (Const.ACTION_GREEN_LED_OFF.equals(action)) {
                mIOHandler.noticeGreenLedOff(context);
            } else if (Const.ACTION_START_SLIENT_INSTALL.equals(action)) {
                String path = intent.getStringExtra("KEY_PATH");
                mIOHandler.handleSlientInstall(path);
            }
        }
    }
}
